
var reportId = '';

function getQueryString(name) {
	var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
	var r = window.location.search.substr(1).match(reg);
	if(r != null) {
		return unescape(r[2]);
		//return decodeURI(r[2]); //解决中文乱码问题
	}
}
var funcTab = {"authNego":"认证协商","encryptNego":"加密协商","encrypt":"加密","exception":"异常检测","virus":"防病毒","resend":"通信重传","integrity":"完整性校验"};
var testSeq = ["authNegoResult","encryptNegoResult","exceptionResult","integrityResult","encryptResult","resendResult"];
var timeLimitTab = {};
var tNumsTab = {};
var attacksTab = {};
var attacksStr = "";
var paramDesignId = getQueryString("designId");
var paramConfigId = getQueryString("configId");
var paramName = getQueryString("protocolName");




function listConfigInfo(configId){
	$.ajax({
		url: HOST.concat("/protocolConfigs/id?id="+configId+"&token=" + config.token),
		type: "GET",
		success: function(datas){
			checkLogin(datas);
			console.log(datas);
			setFuncBtn(datas);
		},
		error: function(){
		    console.log("请求配置详情错误！");
			window.location.href="testInstChoose.html";
		},
		dataType: "json",
	});
}
var funcs = new Array();

function testGetReportDetail(){
    getReportDetail(reportId,getQueryString('designId'));
}

function setFuncBtn(res){
	if(res.meta.state==="success") {

		var configInfo = res.data;
		 if(configInfo["authType"]==""||typeof (configInfo["authType"])=="undefined"){
			$("#authNegoBtn").attr("disabled","disabled");
			$("#authNegoBtn").css("display","none");
		}else{
			funcs.push("authNego");
		}
		if(configInfo["encrypt"]==null||typeof (configInfo["encrypt"])=="undefined"||configInfo["encrypt"]["algorithm"]==""){
			$("#encryptNegoBtn").attr("disabled","disabled");
			$("#encryptNegoBtn").css("display","none");
			$("#encryptBtn").attr("disabled","disabled");
			$("#encryptBtn").css("display","none");
		}else{
			funcs.push("encryptNego");
		}
		if(configInfo["exception"]==null||typeof (configInfo["exception"])=="undefined"||configInfo["exception"]["method"]==""){
			$("#exceptionBtn").attr("disabled","disabled");
			$("#exceptionBtn").css("display","none");
		}else{
			funcs.push("exception");
		}
        if(configInfo["integrity"]==null||typeof (configInfo["integrity"])=="undefined"||configInfo["integrity"]["algorithm"]==""){
            $("#integrityBtn").attr("disabled","disabled");
            $("#integrityBtn").css("display","none");
        }else{
            funcs.push("integrity");
        }
        if(configInfo["encrypt"]==null||typeof (configInfo["encrypt"])=="undefined"||configInfo["encrypt"]["algorithm"]==""){
            $("#encryptNegoBtn").attr("disabled","disabled");
            $("#encryptNegoBtn").css("display","none");
            $("#encryptBtn").attr("disabled","disabled");
            $("#encryptBtn").css("display","none");
        }else{
            funcs.push("encrypt");
        }
		if(configInfo["resend"]==null||typeof (configInfo["resend"])=="undefined"||configInfo["resend"]["queue"]==""){
			$("#resendBtn").attr("disabled","disabled");
			$("#resendBtn").css("display","none");
		}else{

			funcs.push("resend");
		}
        if(configInfo["virus"]==null||typeof (configInfo["virus"])=="undefined"){
            $("#virusBtn").attr("disabled","disabled");
            $("#virusBtn").css("display","none");
        }else{
            $("#virusBtn").attr("disabled","disabled");
            $("#virusBtn").css("display","none");
            //funcs.push("virus");
        }
		$(".tablinks").not(":disabled").click();
	}else{
		window.location.href="testInstChoose.html";
	}
}
var getInputInfo = function(){
    tNumsTab = {};
    timeLimitTab = {};
    attacksTab = {};
    attacksStr = "";
    for(var func in funcs){
        var tNum = $("#"+funcs[func]+"TNum option:selected").val();
        tNumsTab[funcs[func]] = tNum;
        var timeLimit = $("#"+funcs[func]+"Time").val();
        timeLimitTab[funcs[func]] = timeLimit;
        if(func!="encryptNego"){
            if ($("#"+funcs[func]+"AttackOpen option:selected").val() == "Y") {
                attacksTab[funcs[func]] = "1";
                attacksStr = attacksStr+funcs[func]+",";
            }else {
                attacksTab[funcs[func]] = "0";
            }
        }
    }
}

var FuncTest = function(funcName,result,time){
    this.funcName = funcName;
    this.result = result;
    this.time = time;
}
function getLength(obj){

    var count = 0;
    for(var i in obj){
        count++;
    }
    return count;
}
function getTestsTitleInfo(funcName){
    var n = funcs.indexOf(funcName);
    var text = "";
    for(var i=n+1;i<funcs.length;i++){
        text = text+funcTab[funcs[i]]+"测试 "+tNumsTab[funcs[i]]+" 次,"
    }
    return text;
}
var testAuthNego = function(paramDesignId,testNum,timeLimit,attack){
    var text = [];
    var limitLength = 0;
    $.ajax({
        url: HOST.concat("/authNegoTest?id="+paramDesignId+"&testNum="+testNum.toString()+"&attack="+attack.toString()+"&token=" + config.token),
        type: "GET",
        async: false,
        success: function(datas){
            checkLogin(datas);
            if(datas.meta.state==="success"){
                console.log(datas);
                var text0 = "> 进行认证协商测试，共"+getLength(datas.data)+"次,等待测试:"+getTestsTitleInfo("authNego")+"完整测试 1 次";
                limitLength = limitLength + text0.length;
                text.push(text0);
                for(var i in datas.data){
                    var result = datas.data[i].replace(/\u0000/g,"");

                    var text1 = "> 第"+(parseInt(i)+parseInt(1)).toString()+"次认证协商测试结束！";
                    limitLength = limitLength+text1.length;
                    if(attack=="0"){
                        var tag = result.indexOf("攻击类型选择");
                        result = result.substring(0,tag-1);
                    }
                    text.push(text1);
                    var text2 = "> "+result;
                    limitLength = limitLength+text2.length;
                    text.push(text2);
                    if(result!="inner error"){
                        var time = result.split(",")[2].split(":")[1].trim();
                        time = time.substring(0,time.indexOf("ms")-1);

                        if(time!=-1&&parseFloat(time)>parseFloat(timeLimit)){
                            var text3 = "> 超过设置容忍时间: "+timeLimit+" ms";
                            limitLength = limitLength+text3.length;
                            text.push(text3);
                        }
                    }

                    var text4 = "准备开始下一项测试..........     ";
                    limitLength = limitLength+text4.length;
                    text.push(text4);

                }
            }
        },
        error: function(){
            console.log("请求测试错误！");
            var errorText = "> 网络不稳定，请求认证协商测试失败！";
            text.push(errorText);
            limitLength = limitLength+errorText.length;
        },
        dataType: "json",
    });
    doTypeWriter(text,speed);
    return limitLength;
}
var testEncryptNego = function(paramDesignId,testNum,timeLimit){
    var text = [];
    var limitLength = 0;
    $.ajax({
        url: HOST.concat("/encryptNegoTest?id="+paramDesignId+"&testNum="+testNum.toString()+"&token=" + config.token),
        type: "GET",
        async: false,
        success: function(datas){
            checkLogin(datas);
            if(datas.meta.state==="success"){
                console.log(datas);
                var text0 = "> 进行加密协商测试，共"+getLength(datas.data)+"次,等待测试:"+getTestsTitleInfo("encryptNego")+"完整测试 1 次";
                limitLength = limitLength + text0.length;
                text.push(text0);
                for(var i in datas.data){
                    var result = datas.data[i].replace(/\u0000/g,"");

                    var text1 = "> 第"+(parseInt(i)+parseInt(1)).toString()+"次加密协商测试结束！";
                    limitLength = limitLength+text1.length;
                    var text2 = "> "+result;
                    limitLength = limitLength+text2.length;
                    text.push(text1);
                    text.push(text2);
                    if(result!="inner error"){
                        var time = result.split(",")[2].split(":")[1].trim();
                        time = time.substring(0,time.indexOf("ms")-1);

                        if(time!=-1&&parseFloat(time)>parseFloat(timeLimit)){
                            var text3 = "> 超过设置容忍时间: "+timeLimit+" ms";
                            limitLength = limitLength+text3.length;
                            text.push(text3);
                        }
                    }

                    var text4 = "准备开始下一项测试..........     ";
                    limitLength = limitLength+text4.length;
                    text.push(text4);
                }
            }
        },
        error: function(){
            console.log("请求测试错误！");
            var errorText = "> 网络不稳定，请求加密协商测试失败！";
            text.push(errorText);
            limitLength = limitLength+errorText.length;
        },
        dataType: "json",
    });
    doTypeWriter(text,speed);
    return limitLength;
}
var testEncrypt = function(paramDesignId,testNum,timeLimit,attack){
    var text = [];
    var limitLength = 0;
    $.ajax({
        url: HOST.concat("/encryptTest?id="+paramDesignId+"&testNum="+testNum.toString()+"&attack="+attack.toString()+"&token=" + config.token),
        type: "GET",
        async: false,
        success: function(datas){
            checkLogin(datas);
            if(datas.meta.state==="success"){
                console.log(datas);
                var text0 = "> 进行加密测试，共"+getLength(datas.data)+"次,等待测试:"+getTestsTitleInfo("encrypt")+"完整测试 1 次";
                limitLength = limitLength + text0.length;
                text.push(text0);
                for(var i in datas.data){
                    var result = datas.data[i].replace(/\u0000/g,"");

                    var text1 = "> 第"+(parseInt(i)+parseInt(1)).toString()+"次加密测试结束！";
                    limitLength = limitLength+text1.length;
                    if(attack=="0"){
                        var tag = result.indexOf("攻击类型选择");
                        result = result.substring(0,tag-1);
                    }
                    var text2 = "> "+result;
                    limitLength = limitLength+text2.length;
                    text.push(text1);
                    text.push(text2);
                    if(result!="inner error"){
                        var time = result.split(",")[2].split(":")[1].trim();
                        time = time.substring(0,time.indexOf("ms")-1);

                        if(time!=-1&&parseFloat(time)>parseFloat(timeLimit)){
                            var text3 = "> 超过设置容忍时间: "+timeLimit+" ms";
                            limitLength = limitLength+text3.length;
                            text.push(text3);
                        }
                    }

                    var text4 = "准备开始下一项测试..........     ";
                    limitLength = limitLength+text4.length;
                    text.push(text4);
                }
            }
        },
        error: function(){
            console.log("请求测试错误！");
            var errorText = "> 网络不稳定，请求加密测试失败！";
            text.push(errorText);
            limitLength = limitLength+errorText.length;
        },
        dataType: "json",
    });
    doTypeWriter(text,speed);
    return limitLength;
}
var testException = function(paramDesignId,testNum,timeLimit,attack){
    var text = [];
    var limitLength = 0;
    $.ajax({
        url: HOST.concat("/exceptionTest?id="+paramDesignId+"&testNum="+testNum.toString()+"&attack="+attack.toString()+"&token=" + config.token),
        type: "GET",
        async: false,
        success: function(datas){
            checkLogin(datas);
            if(datas.meta.state==="success"){
                console.log(datas);
                var text0 = "> 进行异常检测测试，共"+getLength(datas.data)+"次,等待测试:"+getTestsTitleInfo("exception")+"完整测试 1 次";
                limitLength = limitLength + text0.length;
                text.push(text0);
                for(var i in datas.data){
                    var result = datas.data[i].replace(/\u0000/g,"");

                    var text1 = "> 第"+(parseInt(i)+parseInt(1)).toString()+"次异常检测测试结束！";
                    limitLength = limitLength+text1.length;
                    if(attack=="0"){
                        var tag = result.indexOf("攻击类型选择");
                        result = result.substring(0,tag-1);
                    }
                    var text2 = "> "+result;
                    limitLength = limitLength+text2.length;
                    text.push(text1);
                    text.push(text2);
                    if(result!="inner error"){
                        var time = result.split(",")[2].split(":")[1].trim();
                        time = time.substring(0,time.indexOf("ms")-1);

                        if(time!=-1&&parseFloat(time)>parseFloat(timeLimit)){
                            var text3 = "> 超过设置容忍时间: "+timeLimit+" ms";
                            limitLength = limitLength+text3.length;
                            text.push(text3);
                        }
                    }

                    var text4 = "准备开始下一项测试..........     ";
                    limitLength = limitLength+text4.length;
                    text.push(text4);
                }
            }
        },
        error: function(){
            console.log("请求测试错误！");
            var errorText = "> 网络不稳定，请求异常检测测试失败！";
            text.push(errorText);
            limitLength = limitLength+errorText.length;
        },
        dataType: "json",
    });
    doTypeWriter(text,speed);
    return limitLength;
}
var testResend = function(paramDesignId,testNum,timeLimit,attack){
    var text = [];
    var limitLength = 0;
    $.ajax({
        url: HOST.concat("/resendTest?id="+paramDesignId+"&testNum="+testNum.toString()+"&attack="+attack.toString()+"&token=" + config.token),
        type: "GET",
        async: false,
        success: function(datas){
            checkLogin(datas);
            if(datas.meta.state==="success"){
                console.log(datas);
                var text0 = "> 进行通信重传测试，共"+getLength(datas.data)+"次,等待测试:"+getTestsTitleInfo("resend")+"完整测试 1 次";
                limitLength = limitLength + text0.length;
                text.push(text0);
                for(var i in datas.data){
                    var result = datas.data[i].replace(/\u0000/g,"");

                    var text1 = "> 第"+(parseInt(i)+parseInt(1)).toString()+"次通信重传测试结束！";
                    limitLength = limitLength+text1.length;
                    if(attack=="0"){
                        var tag = result.indexOf("攻击类型选择");
                        result = result.substring(0,tag-1);
                    }
                    var text2 = "> "+result;
                    limitLength = limitLength+text2.length;
                    text.push(text1);
                    text.push(text2);
                    if(result!="inner error"){
                        var time = result.split(",")[2].split(":")[1].trim();
                        time = time.substring(0,time.indexOf("ms")-1);

                        if(time!=-1&&parseFloat(time)>parseFloat(timeLimit)){
                            var text3 = "> 超过设置容忍时间: "+timeLimit+" ms";
                            limitLength = limitLength+text3.length;
                            text.push(text3);
                        }
                    }

                    var text4 = "准备开始下一项测试..........     ";
                    limitLength = limitLength+text4.length;
                    text.push(text4);
                }
            }
        },
        error: function(){
            console.log("请求测试错误！");
            var errorText = "> 网络不稳定，请求通讯重传测试失败！";
            text.push(errorText);
            limitLength = limitLength+errorText.length;
        },
        dataType: "json",
    });
    doTypeWriter(text,speed);
    return limitLength;
}
var testIntegrity = function(paramDesignId,testNum,timeLimit,attack){
    var text = [];
    var limitLength = 0;
    $.ajax({
        url: HOST.concat("/integrityTest?id="+paramDesignId+"&testNum="+testNum.toString()+"&attack="+attack.toString()+"&token=" + config.token),
        type: "GET",
        async: false,
        success: function(datas){
            checkLogin(datas);
            if(datas.meta.state==="success"){
                console.log(datas);
                var text0 = "> 进行完整性校验测试，共"+getLength(datas.data)+"次,等待测试:"+getTestsTitleInfo("integrity")+"完整测试 1 次";
                limitLength = limitLength + text0.length;
                text.push(text0);
                for(var i in datas.data){
                    var result = datas.data[i].replace(/\u0000/g,"");

                    var text1 = "> 第"+(parseInt(i)+parseInt(1)).toString()+"次完整性校验测试结束！";
                    limitLength = limitLength+text1.length;
                    if(attack=="0"){
                        var tag = result.indexOf("攻击类型选择");
                        result = result.substring(0,tag-1);
                    }
                    var text2 = "> "+result;
                    limitLength = limitLength+text2.length;
                    text.push(text1);
                    text.push(text2);
                    if(result!="inner error"){
                        var time = result.split(",")[2].split(":")[1].trim();
                        time = time.substring(0,time.indexOf("ms")-1);

                        if(time!=-1&&parseFloat(time)>parseFloat(timeLimit)){
                            var text3 = "> 超过设置容忍时间: "+timeLimit+" ms";
                            limitLength = limitLength+text3.length;
                            text.push(text3);
                        }
                    }

                    var text4 = "准备开始下一项测试..........     ";
                    limitLength = limitLength+text4.length;
                    text.push(text4);
                }
            }
        },
        error: function(){
            console.log("请求测试错误！");
            var errorText = "> 网络不稳定，请求完整性校验测试失败！";
            text.push(errorText);
            limitLength = limitLength+errorText.length;
        },
        dataType: "json",
    });
    doTypeWriter(text,speed);
    return limitLength;
}
var testAll = function(paramDesignId,testAttacks){
    $.ajax({
        url: HOST.concat("/protocolTest?id="+paramDesignId+"&attacks="+testAttacks.toString()+"&token=" + config.token),
        type: "GET",
        async: false,
        timeout: 20000,
        success: function(datas){
            checkLogin(datas);
            console.log(datas);
            showTestResult(datas,testAttacks);
        },
        error: function(){
            console.log("请求测试错误！");
            var t = [];
            var errorText = "> 网络不稳定，请求测试失败！";
            t.push(errorText);
            doTypeWriter(t,speed);
            $("#saveOp").on('click',reqTest);
            $("#changeModel").on('click',changeConModel);
            $("#saveOp").css('background','#40BEA6');
            $("#changeModel").css('background','#40BEA6');
        },
        dataType: "json",
    });
}
function reqTest(){
    $("#saveOp").off('click',reqTest);
    $("#changeModel").off('click',changeConModel);
    $("#stopOp").on('click',stopTest);
    $("#saveOp").css('background','#85e5d3');
    $("#changeModel").css('background','#85e5d3');
    $("#stopOp").css('background','#40BEA6');
    $("#reportP").off('click',testGetReportDetail);
    $("#reportP").css('background','#85e5d3');
    //获取配置参数
    getInputInfo();

    var text = [];
    var limitLength = 0;
    var str1 = "------------协议测试-------------";
    limitLength = limitLength+str1.length;
    text.push(str1);
    var str2 = "> 初始化测试部件..........";
    limitLength = limitLength+str2.length;
    text.push(str2);
    var str3 = "> 开始测试...........";
    limitLength = limitLength+str3.length;
    text.push(str3);
    var str4 = "> 准备开始第一项测试..........                  ";
    limitLength = limitLength+str4.length;
    text.push(str4);
    doTypeWriter(text,speed);

    setTimeout(function(){
        var llength;

        if(funcs.indexOf("authNego")!=-1){
            var tNum = tNumsTab["authNego"];
            var timeLimit = timeLimitTab["authNego"];
            var attack = attacksTab["authNego"];

            llength = testAuthNego(paramDesignId,tNum,timeLimit,attack);
        }

        //加密协商
        setTimeout(function(){
            if(funcs.indexOf("encryptNego")!=-1){
                var tNum = tNumsTab["encryptNego"];
                var timeLimit = timeLimitTab["encryptNego"];

                llength = testEncryptNego(paramDesignId,tNum,timeLimit);

            }else{
                llength = 0;
            }

                //异常
                setTimeout(function(){
                    if(funcs.indexOf("exception")!=-1) {
                        var tNum = tNumsTab["exception"];
                        var timeLimit = timeLimitTab["exception"];
                        var attack = attacksTab["exception"];
                        llength = testException(paramDesignId, tNum,timeLimit,attack);
                    }else{
                        llength = 0;
                    }

                        //完整
                        setTimeout(function(){
                            if(funcs.indexOf("integrity")!=-1) {
                                var tNum = tNumsTab["integrity"];
                                var timeLimit = timeLimitTab["integrity"];
                                var attack = attacksTab["integrity"];
                                llength = testIntegrity(paramDesignId, tNum, timeLimit,attack);
                            }else{
                                llength = 0;
                            }

                                //加密
                                setTimeout(function(){
                                    if(funcs.indexOf("encrypt")!=-1) {
                                        var tNum = tNumsTab["encrypt"];
                                        var timeLimit = timeLimitTab["encrypt"];
                                        var attack = attacksTab["encrypt"];
                                        llength = testEncrypt(paramDesignId, tNum,timeLimit, attack);
                                    }else{
                                        llength = 0;
                                    }

                                        //重传
                                        setTimeout(function(){
                                            if(funcs.indexOf("resend")!=-1) {
                                                var tNum = tNumsTab["resend"];
                                                var timeLimit = timeLimitTab["resend"];
                                                var attack = attacksTab["resend"];
                                                llength = testResend(paramDesignId, tNum, timeLimit,attack);
                                            }else{
                                                llength = 0;
                                            }

                                                //全部
                                                setTimeout(function(){
                                                    if (funcs.length!=0){
                                                        if(attacksStr.substr(attacksStr.length-1,1)==","){
                                                            attacksStr.substr(0,attacksStr.length-1);
                                                        }
                                                        testAll(paramDesignId,attacksStr);
                                                    }
                                                },(speed+4)*llength)
                                        },(speed+4)*llength);

                                },(speed+4)*llength);


                        },(speed+4)*llength);


                },(speed+4)*llength);

            },(speed+4)*llength);

    },(speed+4)*limitLength);

}
var stopTest = function(){
    location.reload();
}

var speed = 10;
var funcTests = [];
//时间格式转换
function add0(m){return m<10?'0'+m:m}
function format(timeStamp)
{
//shijianchuo是整数，否则要parseInt转换
    var time = new Date(timeStamp);
    var y = time.getFullYear();
    var m = time.getMonth()+1;
    var d = time.getDate();
    var h = time.getHours();
    var mm = time.getMinutes();
    var s = time.getSeconds();
    return y+'/'+add0(m)+'/'+add0(d)+' '+add0(h)+':'+add0(mm)+':'+add0(s);
}
function showTestResult(res,testAttacks){

	funcTests = [];
	var allTest = '';
	if(res.meta.state==="success") {
		var text = [];
		var textLength = 0;
		var wholeTime = 0.0;
		var list = res.data;
        reportId = list.testReportId;
        if(reportId!=""){
            $("#reportP").on('click',testGetReportDetail);
            $("#reportP").css('background','#40BEA6');
            $("#stopOp").off('click',stopTest);
            $("#stopOp").css('background','#85e5d3');
        }
        var textTitle = "> 完整测试结束！"
        text.push(textTitle);
        textLength = textLength + textTitle.length;
		for(var i=0;i<funcs.length;i++){

                var funcTest = new FuncTest();
                funcTest.funcName = funcs[i];
                //funcTest.funcName = testSeq[i].substr(0,testSeq[i].length-6);
                var message = list[funcs[i]+"Result"].toString().replace(/\u0000/g,"");
                //console.log(message);
                if(message.split(",")[0].toLowerCase().indexOf("ok")!=-1){
                    funcTest.result = 1;
                    if(message.split(",")[1].toLowerCase().indexOf("but")!=-1){
                        funcTest.result = 2;
                    }
                }else {
                    funcTest.result = 0;
                }
                var time = message.split(",")[2].split(":")[1].trim();
                time = time.substring(0,time.indexOf("ms")-1);
                funcTest.time = time;
                wholeTime = wholeTime + parseFloat(time);
                //funcTests.push(funcTest);
                var str1 = "> "+funcTab[funcTest.funcName]+"测试结束";
                textLength = textLength + str1.length;
                text.push(str1);
                //text.push("> "+message.substr(0,100));
                if(funcTest.funcName!="encryptNego"&&testAttacks.indexOf(funcTest.funcName)==-1){
                    var tag = message.indexOf("攻击类型选择");
                    message = message.substring(0,tag-1);
                }
                var str2 = "> "+message;
                textLength = textLength + str2.length;
                text.push(str2);
                var limitTime = timeLimitTab[funcTest.funcName];
                if(limitTime!=undefined&&parseFloat(funcTest.time)>parseFloat(limitTime)){
                    var str3 = "> 超过容忍时间"+timeLimitTab[funcTest.funcName]+"ms!";
                    textLength = textLength + str3.length;
                    text.push(str3);
                    funcTest.result = 0;
                }
                if(funcTest.result==1){
                    var str4 = "> 测试成功!";
                    textLength = textLength + str4.length;
                    text.push(str4);
                }else if(funcTest.result==2){
                    var str5 = "> 测试超时!";
                    textLength = textLength + str5.length;
                    text.push(str5);
                    allTest += '2';
                    funcTest.result=0;
                }else {
                    var str6 = "> 测试失败!";
                    textLength = textLength + str6.length;
                    text.push(str6);
                    allTest += '0';
                }
        }
        if(allTest!=''){
			var strall = "完整测试失败!";
            textLength = textLength + strall.length;
            text.push(strall);
		}else {
            var strall = "完整测试成功!";
            textLength = textLength + strall.length;
            text.push(strall);
		}
        var str8 = "完整测试总耗时："+wholeTime.toFixed(3)+" ms";
        textLength = textLength + str8.length;
        text.push(str8);
        var str7 = "--------------测试结束--------------";
        textLength = textLength + str7.length;
        text.push(str7);
		doTypeWriter(text,speed);
		setTimeout(function () {
            $("#saveOp").on('click',reqTest);
            $("#changeModel").on('click',changeConModel);
            $("#saveOp").css('background','#40BEA6');
            $("#changeModel").css('background','#40BEA6');
        },(speed+4)*textLength);
        //yuchuli
        $.ajax({
            url: HOST.concat("/preMessage?designId="+paramDesignId+"&token=" + config.token),
            type: "GET",
            success: function(datas){
                checkLogin(datas);
                console.log(datas);
            },
            error: function(){
                console.log("preMessage error！");
            },
            dataType: "json",
        });

	}else{
		var text = [];
		var errorText = "> ERROR! 测试过程出现故障!";
		text.push(errorText);
		doTypeWriter(text,speed);
        $("#saveOp").on('click',reqTest);
        $("#changeModel").on('click',changeConModel);
        $("#saveOp").css('background','#40BEA6');
        $("#changeModel").css('background','#40BEA6');
	}
}
function doTypeWriter(text, speed) {
	$('#typeWriter').typewrite({
			text: text,
			speed: speed
		}
	);
}



function checkLimitTime(id) {
	var time = $("#"+id).val();
    if(time!=''&&time!=undefined){
        var reg = /^\d+(\.\d+)?$/;
        if(reg.test(time)!=true){
            alert("容忍时间输入不规范");
            $("#"+id).val('');
            return false;
        }else {
            return true;
        }
    }
}

function changeConModel() {
    $("#saveOp").off('click',reqTest);
    $("#changeModel").off('click',changeConModel);

    $("#saveOp").css('background','#85e5d3');
    $("#changeModel").css('background','#85e5d3');
    $("#reportP").off('click',testGetReportDetail);
    $("#reportP").css('background','#85e5d3');
    var text = [];
    var textLength = 0;
    var str1  = "> 正在进行模式切换......";
    var str2  = "> 过程可能会持续几十秒，请耐心等待!";
    textLength += str1.length;
    textLength += str2.length;
    text.push(str1);
    text.push(str2);
    text.push(">");
    doTypeWriter(text,speed);
    setTimeout(function () {
        $.ajax({
            url: HOST.concat("/model/protocolName?protocolName="+paramName+"&token=" + config.token),
            type: "GET",
            async: false,
            timeout : 30000,
            success: function(datas){
                checkLogin(datas);
                console.log(datas);
                if(datas.meta.state=="success"){
                    var text = [];
                    var str1 = "> 模式初始化/切换成功！";
                    var str2 = "> 请确保Ping通后，再点击测试！";
                    text.push(str1);
                    text.push(str2);
                    doTypeWriter(text,speed);
                }else{
                    var text = [];
                    var str = "> ERROR! 切换模式出现故障!";
                    text.push(str);
                    doTypeWriter(text,speed);
                }
                $("#changeModel").on('click',changeConModel);
                $("#saveOp").on('click',reqTest);
                $("#saveOp").css('background','#40BEA6');
                $("#changeModel").css('background','#40BEA6');
            },
            error: function(XMLHttpRequest, textStatus, errorThrown){
                // 状态码
                console.log(XMLHttpRequest.status);
                // 状态
                console.log(XMLHttpRequest.readyState);
                // 错误信息
                console.log(textStatus);
                var text = [];
                var errorText = "> 请确保Ping通后，再点击测试！";
                text.push(errorText);
                doTypeWriter(text,speed);
                $("#changeModel").on('click',changeConModel);
                $("#saveOp").on('click',reqTest);
                $("#saveOp").css('background','#40BEA6');
                $("#changeModel").css('background','#40BEA6');
            },
            dataType: "json",
        });
    },(speed+4)*textLength);
}
